package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.data.OrderDetail;
import com.cskaoyan.bean.data.ResRefundData;
import com.cskaoyan.bean.data.ResShipData;
import com.cskaoyan.bean.data.RespOrderDetail;
import com.cskaoyan.mapper.*;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    @Override
    public CommonData<MarketOrder> list(
            Integer userId, String orderSn,
            Date start, Date end, List<Short> orderStatusArray,
            Integer page, Integer limit, String sort, String order) {

        TransactionUtil.startTransaction();
        MarketOrderMapper marketOrderMapper = TransactionUtil.getMapper(MarketOrderMapper.class);
        //开启分页
        PageHelper.startPage(page, limit);
        TransactionUtil.startTransaction();
        //执行查询
        //构造Example对象，存放条件容器
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        //添加条件
        example.setOrderByClause(sort + " " + order);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (start != null && end != null) {
            criteria.andEndTimeBetween(start,end);
        }
        if(orderStatusArray.size()!=0){
            criteria.andOrderStatusIn(orderStatusArray);
        }
        //查询数据
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);

        TransactionUtil.commit();
        //封装数据
        return CommonData.data(new PageInfo(marketOrders));
    }

    @Override
    public RespOrderDetail detail(Integer id) {
        TransactionUtil.startTransaction();
        //根据用户id查询order   orderGoods    user
        MarketOrderMapper marketOrderMapper = MyBatisUtil.getMapper(MarketOrderMapper.class);
        //用一个data接收 ，接收order，orderGoods，user
        RespOrderDetail respOrderDetail = new RespOrderDetail();
        OrderDetail orderDetail = marketOrderMapper.selectOrderDetail(id);
        System.out.println(orderDetail);
        respOrderDetail.setOrder(orderDetail.getOrder());
        respOrderDetail.setUser(orderDetail.getUser());
        respOrderDetail.setOrderGoods(orderDetail.getOrderGoods());
        TransactionUtil.commit();
        return respOrderDetail;
    }

    @Override
    public boolean refund(ResRefundData resRefundData) {
        TransactionUtil.startTransaction();
        Integer orderId = resRefundData.getOrderId();
        Integer refundMoney = resRefundData.getRefundMoney();
        //更具orderId进行退款操作

        //查询到该条数据，修改该条数据的状态203已退款
        //获取共有的getSqlSession
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketOrderMapper marketOrderMapper = sqlSession.getMapper(MarketOrderMapper.class);

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderId);
        //根据id获取该条order
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        //获取更新后的order
        MarketOrder record = marketOrders.get(0);
        //更新退款后的订单信息
        Short orderStatu = new Short("203");

        record.setOrderStatus(orderStatu);
        int i = marketOrderMapper.updateByPrimaryKey(record);
        System.out.println("更新了"+i+"条数据");

        //更新商品货物

        //1.根据orderId去order_goods获取goods数量，goodsId
        MarketOrderGoodsMapper orderGoodsMapper = sqlSession.getMapper(MarketOrderGoodsMapper.class);
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria marketOrderGoodsExampleCriteria = marketOrderGoodsExample.createCriteria();
        marketOrderGoodsExampleCriteria.andOrderIdEqualTo(orderId);
        MarketOrderGoods marketOrderGoods = orderGoodsMapper.selectByExample(marketOrderGoodsExample).get(0);
        int goodsId = marketOrderGoods.getGoodsId();
        Short number = marketOrderGoods.getNumber();

        //2.根据goodsId更新goods_product数量
        //一个order对应多个product

        MarketGoodsProductMapper productMapper = sqlSession.getMapper(MarketGoodsProductMapper.class);
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria marketGoodsProductExampleCriteria = marketGoodsProductExample.createCriteria();
        marketGoodsProductExampleCriteria.andGoodsIdEqualTo(goodsId);
        List<MarketGoodsProduct> marketGoodsProducts = productMapper.selectByExample(marketGoodsProductExample);
        for (MarketGoodsProduct marketGoodsProduct : marketGoodsProducts) {
            marketGoodsProduct.setNumber(number.intValue()+marketGoodsProduct.getNumber());
            productMapper.updateByPrimaryKey(marketGoodsProduct);
        }
        //返回优惠券

        //根据orderId获取userId
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria orderExampleCriteria = marketOrderExample.createCriteria();
        orderExampleCriteria.andIdEqualTo(orderId);
        MarketOrder order = marketOrderMapper.selectByExample(marketOrderExample).get(0);
        //在根据userId获取couponId
        Integer userId = order.getUserId();
        MarketCouponUserMapper couponUserMapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria marketCouponUserExampleCriteria = marketCouponUserExample.createCriteria();
        marketCouponUserExampleCriteria.andUserIdEqualTo(userId);
        MarketCouponUser marketCouponUser = couponUserMapper.selectByExample(marketCouponUserExample).get(0);
        Integer couponId = marketCouponUser.getCouponId();
        //根据couponId返回数量
        MarketCouponMapper couponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria couponExampleCriteria = marketCouponExample.createCriteria();
        couponExampleCriteria.andIdEqualTo(couponId);
        MarketCoupon marketCoupon = couponMapper.selectByExample(marketCouponExample).get(0);
        marketCoupon.setTotal(marketCoupon.getTotal()+1);
        couponMapper.updateByPrimaryKey(marketCoupon);
        sqlSession.commit();
        sqlSession.close();
        if(i==1){
            return true;
        }
        return false;
    }

    @Override
    public void ship(ResShipData resShipData) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketOrderMapper marketOrderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        //获取orderId，shipChannel，shipSn
        Integer orderId = resShipData.getOrderId();
        String shipChannel = resShipData.getShipChannel();
        String shipSn = resShipData.getShipSn();
        //根据id查询该订单,修改订单信息
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderId);
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        MarketOrder record = marketOrders.get(0);
        record.setShipChannel(shipChannel);
        record.setShipSn(shipSn);
        record.setShipChannel(resShipData.getShipChannel());
        //更新状态
        Short orderStatu = new Short("301");
        record.setOrderStatus(orderStatu);
        //更新该条数据
        int i = marketOrderMapper.updateByPrimaryKey(record);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(Integer orderId) {
        TransactionUtil.startTransaction();
        //删除订单
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketOrderMapper marketOrderMapper = sqlSession.getMapper(MarketOrderMapper.class);

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderId);
        MarketOrder marketOrder = marketOrderMapper.selectByExample(example).get(0);
        marketOrder.setDeleted(true);
        marketOrderMapper.updateByPrimaryKey(marketOrder);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Short getStatus(Integer orderId) {
        TransactionUtil.startTransaction();
        MarketOrderMapper mapper = TransactionUtil.getMapper(MarketOrderMapper.class);
        MarketOrder marketOrder = mapper.selectByPrimaryKey(orderId);
        TransactionUtil.commit();
        return marketOrder.getOrderStatus();
    }

}
